<?php
namespace V2;
class C_CustomerFeedback extends \ResourceClass
{
    public $isActive = true;
    public $remark = '客户端意见反馈';

    /*
     * 参数说明
     * uid 反馈人id
     * content 反馈内容
     * userType(1企业客户，2企业用户)
     * */
    public function post(array $req, array $post, &$result)
    {
        try {
            if (!isset($req['uid'])) {
                return 'require user uid';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            $user_cls = \Biz\UserHandler::getById($req['userType'], $req['uid']);
            if (!is_object($user_cls)) {
                return 'data illegal';
            }
            $enterprise_id = $user_cls->getEnterpriseId();
            if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $enterprise_id, $req['t'] . $post['payload'], $req['k']) == false) {
                return 'data illegal';
            }

            $data = base64_decode($post['payload']);
            @parse_str($data, $data);
            if (!array_key_exists('content', $data)) {
                return 'require content info';
            }
            $user_id = (int)$req['uid'];
            $user_type = (int)$req['userType'];
            $content = (string)$data['content'];
            $feedback = new \Cls\User\CustomerFeedback();
            $feedback->setId($user_id);
            $feedback->setUser($user_type);
            $feedback->setEnterpriseId($enterprise_id);
            $feedback->setContent($content);
            $feedback_hand = new \Biz\CustomerFeedbackHandler();
            $feedback = $feedback_hand->insert($feedback);
            $array_len = count($data);
            for ($i = 1; $i < $array_len; $i++) {
                $img = 'img' . $i;
                if (strlen($data[$img]) > 0) {
                    $this->saveImg($data[$img], $feedback);
                }
            }
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * 获取单个回复详情
     * @param array $req
     * @param array $post
     * @param $result
     */
    public function get(array $req, array $post, &$result)
    {
        try {
            /* 授权验证【开始】 */
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            if (!array_key_exists('id', $req) || empty($req['id'])) {
                return 'require resource id';
            }

            $user_cls = \Biz\UserHandler::getById($req['userType'], $req['uid']);
            $enterprise_id = $user_cls->getEnterpriseId();
            /* 授权验证*/
            if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $enterprise_id, $req['id'] . $req['t'] . $post['payload'], $req['k']) == false) {
                return 'data illegal';
            }

            $feedback_handler = new \Biz\CustomerFeedbackHandler();
            $feedback_lis = $feedback_handler->getFeedbackListById((intval($req['id'])));
            //var_dump(count($feedback_lis));
            foreach ($feedback_lis as $feedback_cls) {
                $feedback = array();
                if($feedback_cls->getIsReply() != 0 || strlen($feedback_cls->getReply())){
                    $feedback['request'] = '';
                    $feedback['request_time'] = '';
                    $feedback['response'] = $feedback_cls->getReply();
                    $feedback['response_time'] = $feedback_cls->getReplyTime();
                    $feedback['img_list'] = array();
                    $feedback_list[] = $feedback;
                }
                //$feedback['feedback_id'] = $feedback_cls->getId();
                $feedback['request'] = $feedback_cls->getContent();
                $feedback['request_time'] = $feedback_cls->getInsertTime();
                $feedback['response'] = '';
                $feedback['response_time'] = '';
                if (is_object($feedback_cls) && $feedback_cls->getFeedbackImageList() != false) {
                    foreach ($feedback_cls->getFeedbackImageList() as $feedbackimg) {
                        $imglist = array(
                            'imgurl' => 'http://image.freshqiao.com/'.$feedbackimg->getUrl(),
                        );
                        $feedback['img_list'][] = $imglist;
                    }
                } else {
                    $feedback['img_list'] = array();
                }
                $feedback_list[] = $feedback;

            }

            $result['feedback_list'] = $feedback_list;

        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * 用户回复
     * @param array $req
     * @param array $post
     * @param $result
     */
    public function put(array $req, array $post, &$result)
    {
        try {
            if (!isset($req['uid'])) {
                return 'require user uid';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            $user_cls = \Biz\UserHandler::getById($req['userType'], $req['uid']);
            if (!is_object($user_cls)) {
                return 'data illegal';
            }
            $enterprise_id = $user_cls->getEnterpriseId();
            if (!array_key_exists('id', $req) || empty($req['id'])) {
                return 'require resource id';
            }
            if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $enterprise_id, $req['id'] . $req['t'] . $post['payload'], $req['k']) == false) {
                return 'data illegal';
            }

            $data = base64_decode($post['payload']);
            @parse_str($data, $data);
            $user_id = (int)$req['uid'];
            $user_type = (int)$req['userType'];
            $feedback_id = (int)$req['id'];
            switch ($data['actionType']) {
                case "halt":
                    $feedback_hand = new \Biz\CustomerFeedbackHandler();
                    $feedback_cls = $feedback_hand->getCustomerFeedbackById($feedback_id);
                    $feedback_cls->setIsReply(3);
                    $result = $feedback_hand->update($feedback_cls);
                    break;
                case "reply":
                    if (!array_key_exists('content', $data)) {
                        return 'require content info';
                    }
                    $content = (string)$data['content'];
                    $feedback = new \Cls\User\CustomerFeedback();
                    $feedback->setFeedbackId($feedback_id);
                    $feedback->setId($user_id);
                    $feedback->setUser($user_type);
                    $feedback->setEnterpriseId($enterprise_id);
                    $feedback->setContent($content);
                    $feedback_hand = new \Biz\CustomerFeedbackHandler();
                    $feedback = $feedback_hand->insert($feedback);
                    $array_len = count($data);
                    for ($i = 1; $i < $array_len-1; $i++) {
                        $img = 'img' . $i;
                        if (strlen($data[$img]) > 0) {
                            $this->saveImg($data[$img], $feedback);
                        }
                    }
                    break;
                default:
                    return 'unkown options';
            }
        } catch (Exception $e) {
            return $e->getMessage();
        }

    }

    private function saveImg($img, $feedback)
    {
        $feedback_img = new \Cls\User\CustomerFeedbackImg();
        $feedback_hand = new \Biz\CustomerFeedbackHandler();
        $feedback_img->setFeedbackId($feedback);
        $feedback_img->setUrl($this->subImg($img));
        $feedback_hand->img_insert($feedback_img);
    }

    private function subImg($img)
    {
        require_once ROOT . 'lib/alioss/sdk.class.php';
        require_once ROOT . 'lib/alioss/util/oss_util.class.php';
        $file_name = $this->guid() . '.jpg';
        $oss = new \ALIOSS(OSS_ACCESS_ID, OSS_ACCESS_KEY, OSS_ENDPOINT);
        $bucket = OSS_BUCKET_XQ;

        $img = str_replace('data:image/png;base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $content = base64_decode($img);

        $options = array(
            'content' => $content,
            'length' => strlen($content),
            \ALIOSS::OSS_HEADERS => array(
                'Content-Type' => 'image/pjpeg')
        );
        $res = $oss->upload_file_by_content($bucket, 'images/' . $file_name, $options);
        //$msg = "上传本地到 /" . $bucket . "/" . $file_name;
        //OSSUtil::print_res($res, $msg);
        if ($res->isOk()) {
            return  'images/' . $file_name;
        }
    }

    private function guid()
    {
        mt_srand((double)microtime() * 10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = substr($charid, 0, 8) . $hyphen
            . substr($charid, 8, 4) . $hyphen
            . substr($charid, 12, 4) . $hyphen
            . substr($charid, 16, 4) . $hyphen
            . substr($charid, 20, 12);
        return $uuid;
    }


}